package com.example.aircraft.database;


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.aircraft.MyApplication;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 包含对数据库的操作工具
 */
public class MySqlManager {
    private static final String TAG = "操作成功";
    private static MySqliteHelper helper;

    /**
     * 单例模式建立 helper
     */
    public static MySqliteHelper getInstance(Context context) {
        if (helper == null) {
            helper = new MySqliteHelper(context);
        }
        return helper;
    }

    /**
     * 查找
     * return Cursor
     */
    public static Cursor selectSQL(SQLiteDatabase db, String sql, String[] selectionArgs) {
        Cursor cursor = null;
        if (db != null) {
            cursor = db.rawQuery(sql, selectionArgs);
        }
        return cursor;
    }

    /**
     * 删改数据库
     *
     * @param db  数据库对象
     * @param sql 删改sql语句
     */
    public static void execSQL(SQLiteDatabase db, String sql) {
        if (db != null) {
            if (sql != null && !"".equals(sql)) {
                db.execSQL(sql);
            }
        }
    }

    /**
     * cursor To list
     */
    public static ArrayList<Rank_Data> cursor1ToList(Cursor cursor) {
        ArrayList<Rank_Data> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            int columnIndx = cursor.getColumnIndex(SQLiteInterface.ID);
            int id = cursor.getInt(columnIndx);
            @SuppressLint("Range")
            String user = cursor.getString(cursor.getColumnIndex(SQLiteInterface.USER));
            @SuppressLint("Range")
            int score = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.SCORE));
            @SuppressLint("Range")
            String time = cursor.getString(cursor.getColumnIndex(SQLiteInterface.TIME));
            @SuppressLint("Range")
            int level = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.LEVEL));
            Rank_Data rank_data = new Rank_Data(id, user, score, time, level);
            list.add(rank_data);
        }
        return list;
    }

    public static ArrayList<User_Data> cursor2ToList(Cursor cursor) {
        ArrayList<User_Data> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            @SuppressLint("Range")
            String user = cursor.getString(cursor.getColumnIndex(SQLiteInterface.USER_NAME));
            @SuppressLint("Range")
            int credits = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.CREDITS));
            @SuppressLint("Range")
            int sheild = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.SHIELD));
            @SuppressLint("Range")
            int cluster = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.CLUSTER));
            @SuppressLint("Range")
            int hemo = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.HEMOPHAGIA));

            User_Data user_data = new User_Data(user, credits, sheild, cluster, hemo);
            list.add(user_data);
        }
        return list;
    }

    public static List<String[]> cursor1ToStrings(Cursor cursor) {
        List<String[]> strings = new LinkedList<>();
        while (cursor.moveToNext()) {
            int columnIndx = cursor.getColumnIndex(SQLiteInterface.ID);
            int id = cursor.getInt(columnIndx);
            @SuppressLint("Range")
            String user = cursor.getString(cursor.getColumnIndex(SQLiteInterface.USER));
            @SuppressLint("Range")
            int score = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.SCORE));
            @SuppressLint("Range")
            int level = cursor.getInt(cursor.getColumnIndex(SQLiteInterface.LEVEL));
            String[] string = {id + "", user, score + "", level + ""};//此处有改动
            strings.add(string);
        }
        return strings;
    }

    public static void deleteData(String ID) {
        helper = MySqlManager.getInstance(MyApplication.getContext());
        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete(SQLiteInterface.TABLE1_NAME, "id=?", new String[]{ID});
        db.close();
    }

    public static void updateData(String username) {
        helper = MySqlManager.getInstance(MyApplication.getContext());
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(SQLiteInterface.USER_NAME, username);
        values.put(SQLiteInterface.CREDITS,5000);
        values.put(SQLiteInterface.CLUSTER,0);
        values.put(SQLiteInterface.HEMOPHAGIA,0);
        values.put(SQLiteInterface.SHIELD,0);
        db.insert(SQLiteInterface.TABLE2_NAME,null,values);
        db.close();
    }
    public static void updateDate2(User_Data user_data) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(SQLiteInterface.USER_NAME, user_data.getUSER_NAME());
        contentValues.put(SQLiteInterface.CREDITS, user_data.getCREDITS());
        contentValues.put(SQLiteInterface.SHIELD, user_data.getSHEILD());
        contentValues.put(SQLiteInterface.CLUSTER, user_data.getCLUSTER());
        contentValues.put(SQLiteInterface.HEMOPHAGIA, user_data.getHEMOPHAGIA());

        helper = MySqlManager.getInstance(MyApplication.getContext());
        SQLiteDatabase db = helper.getWritableDatabase();
        int i = db.update(SQLiteInterface.TABLE2_NAME, contentValues, "USER_NAME=?", new String[]{user_data.getUSER_NAME()});
        db.close();

    }

    public static User_Data getData2(String userName) {   //用于获取一个用户USER_DATA数据并将其对象化
        helper = MySqlManager.getInstance(MyApplication.getContext());
        SQLiteDatabase db = helper.getReadableDatabase();
        String selectsql = "select * from " + SQLiteInterface.TABLE2_NAME;
        List<User_Data> userList = MySqlManager.cursor2ToList(MySqlManager.selectSQL(db, selectsql, null));
        for (User_Data user : userList) {
            Log.d("User",user.getUSER_NAME());
            if (user.getUSER_NAME().equals(userName)) {
                db.close();
                return user;
            }
        }
        db.close();
        return null;
    }

}
